<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\GoodsParamsRepositories;
use Illuminate\Http\Request;

class GoodsParamsController extends BaseController
{
    public function __construct(GoodsParamsRepositories $goodsParamsRepositories)
    {
        $this->goodsParamsRepositories = $goodsParamsRepositories;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.goodsparams.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.goodsparams.create')->render();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $ret = $this->goodsParamsRepositories->save($request->all());

        if($ret)
        {
            return redirect()->route('admin.goods.params')->with(['success'=>'添加完成']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        return view('admin.goodsparams.edit')->render();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
       $ids=\request()->input('ids');
       $ret =$this->goodsParamsRepositories->delete($ids);
       if($ret)
       {
           return response()->json([
               'code' => 0,
               'msg'   => '删除成功！',
           ]);
       }
        return response()->json(['ok'=>222]);
    }

    public function data(Request $request)
    {
        $data = $request->all();
        $res = $this->goodsParamsRepositories->search($data);
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
}
